﻿using jczUserService.Handlers;
using Nancy;
using Nancy.ModelBinding;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UserService.Business;
using UserService.Entity;

namespace jczUserService.Modules
{
    public class PassUser
    {
        public string UserName { get; set; }
        public string PasswordHash { get; set; }
        public string client_id { get; set; }
    }
    public class AuthModule : NancyModule
    {
        public AuthModule() : base("/auth")
        {
            Post["/login"] = x =>
            {
                string jwt = "";
                //PassUser loginUser = x.Bind<PassUser>();
                string s_uid = Request.Query["uid"];//get请求获取方法
                PassUser loginUser = this.Bind<PassUser>();
                /*if (loginUser.client_id.Equals("PasswordClient"))
                {
                    var reE = new
                    {
                        code = "401",
                        reMessage = "客户端类型错误！"
                    };
                    return Response.AsJson(reE);
                }*/
                using (UserBusiness _repo = new UserBusiness())
                {
                    User user = _repo.FindUser(loginUser.UserName, loginUser.PasswordHash);

                    if (user == null)
                    {
                        var reE = new
                        {
                            code = "402",
                            reMessage = "用户输入错误！"
                        };
                        return Response.AsJson(reE);
                    }
                    else
                    {
                        Client obj = null;
                        using (ClientBusiness _Client_repo = new ClientBusiness())
                        {
                            obj = _Client_repo.FindClient(loginUser.client_id);
                        }
                        if (obj == null)
                        {
                            var reE = new
                            {
                                code = "401",
                                reMessage = "客户端类型错误！"
                            };
                            return Response.AsJson(reE);
                        }
                        else
                        {
                            if (obj.client_url.Equals("json"))
                            {
                                jwt = JwtAuthenticationMessageHandler.CreateJWT(user.ToClaimsIdentity());
                                System.Diagnostics.Debug.WriteLine("loginModule---CreateJWT-------------" + jwt);
                                string _RootPath = AppDomain.CurrentDomain.GetData(".appPath").ToString();
                                ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                                map.ExeConfigFilename = _RootPath + @"\bin\jczUserService.dll.config";
                                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                                string SecretKey = config.AppSettings.Settings["SecretKey"].Value;
                                string RandomKey = config.AppSettings.Settings["RandomKey"].Value;
                                var revalue = new
                                {
                                    code = "200",
                                    jwt = jwt,
                                    SecretKey = SecretKey,
                                    RandomKey = RandomKey,
                                    reMessage = "用户注册成功！"
                                };
                                return Response.AsJson(revalue);
                            }
                            else
                            {
                                var reE = new
                                {
                                    code = "401",
                                    reMessage = "客户端类型错误！"
                                };
                                return Response.AsJson(reE);
                            }
                            
                        }

                        //Session["logintoken"] = jwt;
                    }
                }
                
            };
            Post["/tokenkey"] = x =>
            {
                string _RootPath = AppDomain.CurrentDomain.GetData(".appPath").ToString();
                ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                map.ExeConfigFilename = _RootPath + @"\bin\jczUserService.dll.config";
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                string SecretKey = config.AppSettings.Settings["SecretKey"].Value;
                string RandomKey = config.AppSettings.Settings["RandomKey"].Value;
                var revalue = new
                {
                    SecretKey = SecretKey,
                    RandomKey = RandomKey
                };
                return Response.AsJson(revalue);
            };
            Get["/AddClient"] = _ =>
            {
                Client obj = this.Bind<Client>();
                /*Client obj = new Client()
                {
                    client_id = "rs_client",
                    client_url = "http://192.168.0.8:3009/views/rs_data.html",
                    client_secret = "jcz123",
                    client_code = "",
                    client_scope = ""
                };*/
                bool b_add = false;
                using (ClientBusiness _repo = new ClientBusiness())
                {
                    b_add = _repo.InstertClient(obj);
                }
                if (b_add) return Response.AsText("成功", "text/html;charset=UTF-8");
                return Response.AsText("失败", "text/html;charset=UTF-8");
            };
        }
    }
}
